<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 4.3.2 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008 - 2010, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */
// ------------------------------------------------------------------------

/**
 * CodeIgniter Project Helpers
 *
 * @package		CodeIgniter
 * @subpackage	Helpers
 * @category	Helpers
 * @author		Austin
 */

// ------------------------------------------------------------------------

/**
 * __debug
 * 
 * @access	public
 * @param   string/array
 * @return	void
 */
if (!function_exists('__debug')) {

  function __debug($data) {
    if (OS_CRM_DEBUG) {
      if (is_array($data)) {
        echo '<pre>';
        print_r($data);;
        echo '</pre>';
      } else {
        __e($data);
      }
    }
  }

}

// ------------------------------------------------------------------------

/**
 * Is Admin Logged In 
 * 
 * @access	public
 * @param	array
 * @param	string
 * @param	boolean
 * @param	string
 * @return	void
 */
if (!function_exists('__earray')) {

  function __earray($data = array(), $element = '', $return = false, $filter = '') {
    $var = '';
    $var = ( isset($data[$element]) ) ? $data[$element] : '';
    
    if(FALSE != $filter && !empty($filter)){
      $var = filter_data($var, $filter);
    }
    
    if (true == $return) {
      return $var;
    } else {
      echo $var;
    }
  }

}

// ------------------------------------------------------------------------

/**
 * __E  - echo a statement
 *
 * 
 * @access	public
 * @param	string	statement
 * @param	boolean	if config var
 * @param	boolean	if return var
 * @return	void
 */
if (!function_exists('__e')) {

  function __e($data = '', $conf = false, $return = false) {
    $CI = &get_instance();
    $var = '';
    if (true == $conf) {
      $var = $CI->config->item($data);
    } else {
      $var = $data;
    }

    if (true == $return) {
      return $var;
    } else {
      echo $var;
    }
  }

}

// ------------------------------------------------------------------------

/**
 * __ERR  - echo the error
 *
 * 
 * @access	public
 * @param	string	statement
 * @param	boolean	if config var
 * @param	boolean	if return var
 * @return	void
 */
if (!function_exists('__err')) {

  function __err($data = '', $return = false) {
    $info = '';

    $info .= '<div id="err_loadme">';
    $info .= '<div class="err_heading" align="left">Errors(s)</div>';
    $info .= '<div class="err_body" align="left">' . $data . '</div>';
    $info .='</div>';

    if (true == $return) {
      return $info;
    } else {
      __e($info);
    }
  }

}

// ------------------------------------------------------------------------

/**
 * __suc  - echo the success
 *
 * 
 * @access	public
 * @param	string	statement
 * @param	boolean	if return var
 * @return	void
 */
if (!function_exists('__suc')) {

  function __suc($data = '', $return = false) {
    $info = '';

    $info .= '<div id="suc_loadme">';
    $info .= '<div class="suc_heading" align="left">Congratulations</div>';
    $info .= '<div class="suc_body" align="left">' . $data . '</div>';
    $info .='</div>';

    if (true == $return) {
      return $info;
    } else {
      __e($info);
    }
  }

}

// ------------------------------------------------------------------------

/**
 * Books Img  - this will give the absolute and relative path for books images
 *
 * 
 * @access	public
 * @param	string	source or file name
 * @param	string  title and alt to the image
 * @param	int     0 = Small, 1 = Medium, 2 = Large
 * @return	string
 */
if (!function_exists('_books_img')) {

  function _books_img($src = '', $title = '', $size = 0) {
    $str = '';
    
    /*get teh sixe of the image*/
    switch ($size) {
      default:
      case 0:
        $w = BOOKS_IMG_S_W;
        $h = BOOKS_IMG_S_H;
        break;

      case 1:
        $w = BOOKS_IMG_M_W;
        $h = BOOKS_IMG_M_H;
        break;

      case 2:
        $w = BOOKS_IMG_L_W;
        $h = BOOKS_IMG_L_H;
        break;
      
      case 3:
        $w = BOOKS_IMG_VS_W;
        $h = BOOKS_IMG_VS_H;
        break;
      
    }
    
    /*common data*/
    $title = (empty($title)) ? OS_CRM_TITLE .' Media Images' : $title;
    $img_param = array(
          'src' => 'images/crm_noimage.jpg',
          'title' => $title,
          'alt' => $title,
          'width' => $w,
          'height' => $h,
          'border' => 0
      );
      
    /*overite the src is image exists*/
    $abs_file = ROOTBASEPATH . BOOKS_IMG . $src;
    //__debug($abs_file );exit;  
    if (is_file( $abs_file )) {
      $img_param['src'] = BOOKS_IMG . $src;
    }
    $str = img($img_param);
    
    return $str;
  }

}


// ------------------------------------------------------------------------

/**
 * Articles Img  - this will give the absolute and relative path for books images
 *
 * 
 * @access	public
 * @param	string	source or file name
 * @param	string  title and alt to the image
 * @param	int     0 = Small, 1 = Medium, 2 = Large
 * @param	int     article id
 * @return	string
 */
if (!function_exists('_articles_img')) {

  function _articles_img($src = '', $title = '', $size = 0, $id = 0) {
    $str = '';
    
    /*get teh sixe of the image*/
    switch ($size) {
      default:
      case 0:
        $w = BOOKS_IMG_S_W;
        $h = BOOKS_IMG_S_H;
        break;

      case 1:
        $w = BOOKS_IMG_M_W;
        $h = BOOKS_IMG_M_H;
        break;

      case 2:
        $w = BOOKS_IMG_L_W;
        $h = BOOKS_IMG_L_H;
        break;
      
      case 3:
        $w = BOOKS_IMG_VS_W;
        $h = BOOKS_IMG_VS_H;
        break;
      
    }
    
    /*common data*/
    $title = (empty($title)) ? OS_CRM_TITLE .' Media Images' : $title;
    $img_param = array(
          'src' => 'images/crm_noimage.jpg',
          'title' => $title,
          'alt' => $title,
          'width' => $w,
          'height' => $h,
          'border' => 0
      );
      
    /*overite the src is image exists*/
    $abs_file = ROOTBASEPATH . MEDIA_ARTICLES . '/'. $id . '/' . $src;
    //__debug($abs_file );exit;  
    if (is_file( $abs_file )) {
      $img_param['src'] = MEDIA_ARTICLES . '/'. $id . '/' . $src;
    }
    $str = img($img_param);
    
    return $str;
  }

}

// ------------------------------------------------------------------------

/**
 * Journals Img  - this will give the absolute and relative path for books images
 *
 * 
 * @access	public
 * @param	string	source or file name
 * @param	string  title and alt to the image
 * @param	int     0 = Small, 1 = Medium, 2 = Large
 * @param	int     article id
 * @return	string
 */
if (!function_exists('_journals_img')) {

  function _journals_img($src = '', $title = '', $size = 0, $id = 0) {
    $str = '';
    
    /*get teh sixe of the image*/
    switch ($size) {
      default:
      case 0:
        $w = BOOKS_IMG_S_W;
        $h = BOOKS_IMG_S_H;
        break;

      case 1:
        $w = BOOKS_IMG_M_W;
        $h = BOOKS_IMG_M_H;
        break;

      case 2:
        $w = BOOKS_IMG_L_W;
        $h = BOOKS_IMG_L_H;
        break;
      
      case 3:
        $w = BOOKS_IMG_VS_W;
        $h = BOOKS_IMG_VS_H;
        break;
      
    }
    
    /*common data*/
    $title = (empty($title)) ? OS_CRM_TITLE .' Media Images' : $title;
    $img_param = array(
          'src' => 'images/crm_noimage.jpg',
          'title' => $title,
          'alt' => $title,
          'width' => $w,
          'height' => $h,
          'border' => 0
      );
      
    /*overite the src is image exists*/
    $abs_file = ROOTBASEPATH . MEDIA_JOURNALS . $id . '/' . $src;
    //__debug($abs_file );exit;  
    if (is_file( $abs_file )) {
      $img_param['src'] = MEDIA_JOURNALS . $id . '/' . $src;
    }
    $str = img($img_param);
    
    return $str;
  }

}


// ------------------------------------------------------------------------

/**
 * Articles File  - this will give the absolute and relative path for files
 *
 * 
 * @access	public
 * @param	string	source or file name
 * @return	string
 */
if (!function_exists('_articles_file')) {

  function _articles_file($src = '',$id = 0) {
    $str = array();        
    $str['file'] = '';
    $str['path'] = '';
    $str['url'] = '';
    /*overite the src is image exists*/
    $abs_file = ROOTBASEPATH . MEDIA_ARTICLES . $id . '/' . $src;
    //__debug($abs_file );exit;  
    if (is_file( $abs_file )) {
      $str['file'] = $src;
      $str['path'] = ROOTBASEPATH . MEDIA_ARTICLES . $id . '/'. $src;
      $str['url'] = base_url() . MEDIA_ARTICLES . $id . '/'. $src;
    }    
    return $str;
  }

}



// ------------------------------------------------------------------------

/**
 * Articles File  - this will give the absolute and relative path for files
 *
 * 
 * @access	public
 * @param	string	source or file name
 * @return	string
 */
if (!function_exists('_articles_file')) {

  function _articles_file($src = '',$id = 0) {
    $str = array();        
    $str['file'] = '';
    $str['path'] = '';
    /*overite the src is image exists*/
    $abs_file = ROOTBASEPATH . MEDIA_ARTICLES . '/'. $id . '/' . $src;
    //__debug($abs_file );exit;  
    if (is_file( $abs_file )) {
      $str['file'] = $src;
      $str['path'] = MEDIA_ARTICLES . '/'. $id . '/'. $src;
    }    
    return $str;
  }

}

// ------------------------------------------------------------------------

/**
 * Output File  
 *
 * 
 * @access	public
 * @param	string	source or file name
 * @return	string
 */
if (!function_exists('output_file')) {

  function output_file($file, $name, $mime_type='',$force_dwl = TRUE) {
    /*
      This function takes a path to a file to output ($file),
      the filename that the browser will see ($name) and
      the MIME type of the file ($mime_type, optional).

      If you want to do something on download abort/finish,
      register_shutdown_function('function_name');
      *********************************************
            Example of use
      **********************************************

      Make sure script execution doesn't time out.
      Set maximum execution time in seconds (0 means no limit).

      set_time_limit(0);
      $file_path='that_one_file.txt';
      output_file($file_path, 'some file.txt', 'text/plain');
     */
    set_time_limit(0);
    
    if (!is_readable($file))
      die('File not found or inaccessible! Please try again later.');

    $size = filesize($file);
    $name = rawurldecode($name);

    /* Figure out the MIME type (if not specified) */
    $known_mime_types = array(
        "pdf" => "application/pdf",
        "txt" => "text/plain",
        "html" => "text/html",
        "htm" => "text/html",
        "exe" => "application/octet-stream",
        "zip" => "application/zip",
        "doc" => "application/msword",
        "xls" => "application/vnd.ms-excel",
        "ppt" => "application/vnd.ms-powerpoint",
        "gif" => "image/gif",
        "png" => "image/png",
        "jpeg" => "image/jpg",
        "jpg" => "image/jpg",
        "php" => "text/plain"
    );

    if ($mime_type == '') {
      $file_extension = strtolower(substr(strrchr($file, "."), 1));
      if (array_key_exists($file_extension, $known_mime_types)) {
        $mime_type = $known_mime_types[$file_extension];
      } else {
        $mime_type = "application/force-download";
      };
    };

    @ob_end_clean(); //turn off output buffering to decrease cpu usage
    // required for IE, otherwise Content-Disposition may be ignored
    if (ini_get('zlib.output_compression'))
      ini_set('zlib.output_compression', 'Off');

    header('Content-Type: ' . $mime_type);
    header('Content-Disposition: attachment; filename="' . $name . '"');
    header("Content-Transfer-Encoding: binary");
    header('Accept-Ranges: bytes');

    /* The three lines below basically make the
      download non-cacheable */
    header("Cache-control: private");
    header('Pragma: private');
    $format = 'DATE_RFC822';
    $time = time();     
    header("Expires: ".standard_date($format, $time));

    // multipart-download and download resuming support
    if (isset($_SERVER['HTTP_RANGE'])) {
      list($a, $range) = explode("=", $_SERVER['HTTP_RANGE'], 2);
      list($range) = explode(",", $range, 2);
      list($range, $range_end) = explode("-", $range);
      $range = intval($range);
      if (!$range_end) {
        $range_end = $size - 1;
      } else {
        $range_end = intval($range_end);
      }

      $new_length = $range_end - $range + 1;
      header("HTTP/1.1 206 Partial Content");
      header("Content-Length: $new_length");
      header("Content-Range: bytes $range-$range_end/$size");
    } else {
      $new_length = $size;
      header("Content-Length: " . $size);
    }

    /* output the file itself */
    $chunksize = 1 * (1024 * 1024); //you may want to change this
    $bytes_send = 0;
    if ($file = fopen($file, 'r')) {
      if (isset($_SERVER['HTTP_RANGE']))
        fseek($file, $range);

      while (!feof($file) &&
      (!connection_aborted()) &&
      ($bytes_send < $new_length)
      ) {
        $buffer = fread($file, $chunksize);
        print($buffer); //echo($buffer); // is also possible
        flush();
        $bytes_send += strlen($buffer);
      }
      fclose($file);
    } else
      die('Error - cannot open file.');

    die();
  }

}

// ------------------------------------------------------------------------

/**
 * __is_loggedin  - Is user Logged
 *
 * 
 * @access	public
 * @param   NA
 * @return  void
 */
if (!function_exists('__is_loggedin')) {

  function __is_loggedin() {
    $CI = &get_instance();    
    $status = FALSE;
    
    $usr_info = $CI->session->userdata('usr_info');
    if(!empty($usr_info['usr_id'])){
      $status = TRUE;
    }
    return $status;
  }
}

// ------------------------------------------------------------------------

/**
 * __get_fuser  - Get frontend user
 *
 * 
 * @access	public
 * @param   NA
 * @return  void
 */
if (!function_exists('__get_fuser')) {

  function __get_fuser() {
    $CI = &get_instance();        
    $usr_info = $CI->session->userdata('usr_info');
    return $usr_info;
  }
}

// ------------------------------------------------------------------------

/**
 * __admin_loggedin  - Is admin Logged
 *
 * 
 * @access	public
 * @param   NA
 * @return  void
 */
if (!function_exists('__admin_loggedin')) {

  function __admin_loggedin() {
    $CI = &get_instance();    
    $status = FALSE;
    
    $usr_info = $CI->session->userdata('adminusr_info');
    if(!empty($usr_info['usr_id']) && ($usr_info['usr_grp_id'] == 1 || $usr_info['usr_grp_id'] == 2)){
      $status = TRUE;
    }
    return $status;
  }
}

// ------------------------------------------------------------------------

/**
 * __get_buser  - Get backend user
 *
 * 
 * @access	public
 * @param   NA
 * @return  void
 */
if (!function_exists('__get_buser')) {

  function __get_buser() {
    $CI = &get_instance();        
    $usr_info = $CI->session->userdata('adminusr_info');
    return $usr_info;
  }
}

// ------------------------------------------------------------------------

/**
 * __loggedin_info  - Logged user info
 *
 * 
 * @access	public
 * @param   string session key
 * @return  string/array
 */
if (!function_exists('__loggedin_info')) {

  function __loggedin_info($key = '') {
    $CI = &get_instance();
    $info = '';
    $info = $CI->session->userdata($key);
    return $info;    
  }
}

// ------------------------------------------------------------------------

/**
 * __display - views header, footer and main
 * 
 * @access	public
 * @param   string
 * @param   array
 * @return  void
 */
if (!function_exists('__display')) {

  function __display($view = '', $data = array()) {    
    $CI = &get_instance();    
    $CI->load->view('header', $data);
		$CI->load->view($view   , $data);
    $CI->load->view('footer', $data);
  }

}

// ------------------------------------------------------------------------

/**
 * Admin display - views header, footer and main
 * 
 * @access	public
 * @param   string
 * @param   array
 * @return  void
 */
if (!function_exists('__admin_display')) {

  function __admin_display($view = '', $data = array()) {    
    $CI = &get_instance();    
    $CI->load->view('adminheader', $data);
		$CI->load->view($view   , $data);
    $CI->load->view('adminfooter', $data);
  }

}

// ------------------------------------------------------------------------

/**
 * __display_data - views header, footer and main
 * 
 * @access	public
 * @param   string
 * @param   array
 * @return  void
 */
if (!function_exists('__display_data')) {

  function __display_data($data = '', $mode = 'display') {
    __e(default_value(filter_data($data,$mode)));
  }

}

// ------------------------------------------------------------------------

/**
 * Get FCK Toolbarset
 * 
 * @access	public
 * @param	array
 * @param	string
 * @param	boolean
 * @return	void
 */
if (!function_exists('get_fck_toolbar')) {

  function get_fck_toolbar() {
    $var = 'Basic';
    $CI = &get_instance();
    $usr_info = $CI->session->userdata('adminusr_info');        
    if(!empty($usr_info['usr_email']) && $usr_info['usr_email'] == OS_CRM_ADMIN_EMAIL){
      $var = 'Default';
    }
    return $var;
  }
}


// ------------------------------------------------------------------------

/**
 * Run custom Query
 * 
 * @access	public
 * @param   NA
 * @return	void
 */
if (!function_exists('run_cust_qry')) {

  function run_cust_qry($qry, $one_row = FALSE) {    
    $var = array();
    $var['list'] = array();
    $var['num_rows'] = 0;
    if (!empty($qry)) {
      $CI = &get_instance();
      $handle = $CI->db->query($qry);
      //__debug($qry);
      $num_rows = $handle->num_rows();
      $var['num_rows'] = $num_rows;
      if ($num_rows > 0) {
        if ($one_row == TRUE) {
          $var['list'] = $handle->row_array();
        } else {
          $result_set = $handle->result_array();
          foreach ($result_set as $row) {
            $var['list'][] = $row;
          }
        }
      }
    }
    //__debug($var);
    return $var;
  }
}

// ------------------------------------------------------------------------

/**
 * Execute custom Query
 * 
 * @access	public
 * @param   NA
 * @return	void
 */
if (!function_exists('exec_cust_qry')) {

  function exec_cust_qry($qry) {
    if (!empty($qry)) {
      $CI = &get_instance();
      $handle = $CI->db->query($qry);
      //__debug($qry);
    }
  }
}

// ------------------------------------------------------------------------

/**
 * Get Left Nav
 * 
 * @access	public
 * @param   NA
 * @return	void
 */
if (!function_exists('get_left_nav')) {

  function get_left_nav() {
    $var = '';
    $limit = 30;
    $qry = "SELECT  *
            FROM ".TBL_CATEGORY."
            WHERE os_category_status = '1'
            ORDER BY os_category_name
            LIMIT $limit 							  			
            ";
		$var = run_cust_qry($qry);
    return $var;
  }
}

// ------------------------------------------------------------------------

/**
 * Get Category
 * 
 * @access	public
 * @param   string fields
 * @return	void
 */
if (!function_exists('get_category')) {

  function get_category($fields = '*') {
    $var = '';
    $limit = 30;
    $qry = "SELECT  ".$fields."
            FROM ".TBL_CATEGORY."
            WHERE os_category_status = '1'
            ORDER BY os_category_name
            LIMIT $limit 							  			
            ";
		$var = run_cust_qry($qry);
    return $var;
  }
}

// ------------------------------------------------------------------------

/**
 * Get Articles
 * 
 * @access	public
 * @param   string fields
 * @return	void
 */
if (!function_exists('get_articles')) {

  function get_articles($fields = '*') {
    $var = '';
    $limit = 30;
    $qry = "SELECT  ".$fields."
            FROM ".TBL_ARTICLES."
            WHERE os_articles_status = '1'
            ORDER BY os_articles_title
            LIMIT $limit 							  			
            ";
		$var = run_cust_qry($qry);
    return $var;
  }
}

// ------------------------------------------------------------------------

/**
 * Get Articles
 * 
 * @access	public
 * @param   string fields
 * @return	void
 */
if (!function_exists('get_editors')) {

  function get_editors($fields = '*') {
    $var = '';
    $limit = 30;
    $qry = "SELECT  ".$fields."
            FROM ".TBL_EDITOR."
            WHERE os_editor_status = '1'
            ORDER BY os_editor_name
            LIMIT $limit 							  			
            ";
		$var = run_cust_qry($qry);
    
    return $var;
  }
}


// ------------------------------------------------------------------------

/**
 * Get Articles
 * 
 * @access	public
 * @param   string fields
 * @return	void
 */
if (!function_exists('get_publishers')) {

  function get_publishers($fields = '*') {
    $var = '';
    $limit = 30;
    $qry = "SELECT  ".$fields."
            FROM ".TBL_PUB."
            WHERE os_pub_status = '1'
            ORDER BY os_pub_name
            LIMIT $limit 							  			
            ";
		$var = run_cust_qry($qry);
    return $var;
  }
}

// ------------------------------------------------------------------------

/**
 * __upload_file - Upload file
 * 
 * @access	public
 * @param	array
 * @param	string
 * @param	boolean
 * @return	void
 */
if (!function_exists('__upload_file')) {

  function __upload_file($uploadfile = '',$frm_field = '') {
    
    //__debug($_FILES);
    if (isset($_FILES[$frm_field]['tmp_name'])) {
      if (move_uploaded_file($_FILES[$frm_field]['tmp_name'], $uploadfile)) {
        //__debug("File is valid, and was successfully uploaded.\n");
        return 1;
      } else {
        //__debug("Possible file upload attack!\n");
        return 0;
      }
    }
  }
}


// ------------------------------------------------------------------------

/**
 * Build Select box
 * 
 * @access	public
 * @param   array
 * @return	void
 * 
 * $list_data['id'] = '';
 * $list_data['value'] = '';
 * $list_data['key'] = '';
 * $list_data['list'] = '';
 * $list_data['sel'] = '';
 */
if (!function_exists('build_select_box')) {

  function build_select_box($list_data = array()) {
    
    $var = '';
    if(count($list_data['list']) > 0){      
      $var .= '<div class="frm_select">';    
      foreach ($list_data['list'] as $key => $value) {
        
        $sel = '';
        $search = $value[$list_data['key']];
        $lis_items = $list_data['sel'];
        if(is_array($lis_items) && in_array($search ,$lis_items )){
          $sel = 'checked="checked"';
        }
        
        $var .= '<div class="frm_options">
                    <input type="checkbox" name="'.$list_data['id'].'[]" id="'.$list_data['id'].$key.'" class="rd" value="'.$value[$list_data['key']].'" '.$sel.'/>              
                    <label for="'.$list_data['id'].$key.'">'.$value[$list_data['value']].'</label>
                  </div>';    
      }
      $var .= '</div>';
    }
    return $var;
  }
}

// ------------------------------------------------------------------------

/**
 * Set Date
 * 
 * @access	public
 * @param   string
 * @param   int
 * @return	void
 * 
 */
if (!function_exists('__set_date')) {

  function __set_date($data = '', $format = 1) {    
    $var = ''; 
    if(!empty ($data)){
      switch ($format){
        case 1:
        default:  
          $var = date('d-m-Y', strtotime($data));
        break;

        case 2:
          $var = date('d-m-Y H:i:s', strtotime($data));
        break;
      
        case 3:
          $var = date('jS M Y H:i:a', strtotime($data));
        break;
      
        case 4:
          $var = date('jS M Y', strtotime($data));
        break;
      }
    }
    return $var;
  }
}

// ------------------------------------------------------------------------

/**
 * Price - based on coutry session
 * 
 * @access	public
 * @param   string
 * @param   int
 * @return	void
 * 
 */
if (!function_exists('__price')) {

  function __price($data = '', $currency_code = '') {    
    
    $var = number_format($data, 2) . ' (INR)';
    $CI = &get_instance();
    
    if(empty($currency_code)){
      $currency_code = $CI->session->userdata('currency_code');
    }
    
    
    if(!empty ($currency_code) && $currency_code != "INR"){
      $inr = $var;
      $qry = "SELECT  os_currency_value
            FROM ".TBL_CURRENCY."
            WHERE os_currency_code 	= '".filter_data($currency_code)."'
            ";
      //__debug(      '<br/>--'.$qry);
      $row = run_cust_qry($qry,TRUE);
      if($row['num_rows'] > 0 && isset($row['list']['os_currency_value']) && $row['list']['os_currency_value'] > 0){
        $var = ($data/$row['list']['os_currency_value']);
        $var = round($var, 2);
        $var = number_format($var, 2). ' ('.$currency_code.')';
      }
    }
    return $var;
  }
}


// ------------------------------------------------------------------------

/**
 * Price - based on coutry session
 * 
 * @access	public
 * @param   string
 * @param   int
 * @return	void
 * 
 */
if (!function_exists('__set_currency_dropdown')) {

  function __set_currency_dropdown() {
    $CI = &get_instance();
    $currency_code = $CI->session->userdata('currency_code');
    $var = '<option value="INR" title="India">INR</option>';
    $qry = "SELECT  *
            FROM ".TBL_CURRENCY."
            WHERE 1
            ";
    //__debug(      '<br/>--'.$qry);
    $row = run_cust_qry($qry);
    if($row['num_rows'] > 0){
      foreach ($row['list'] as $key => $value) {
        $sel = ($currency_code == $value['os_currency_code'])?'selected="selected"':'';          
        $var .= '<option value="'.$value['os_currency_code'].'"  '.$sel.' title="'.$value['os_currency_country'].'">'.$value['os_currency_code'].'</option>';
      }
    }
    return $var;
  }
}

// ------------------------------------------------------------------------

/**
 * Btn Download - based on coutry session
 * 
 * @access	public
 * @param   string
 * @param   int
 * @return	void
 * 
 */
if (!function_exists('__btn_dwl')) {

  function __btn_dwl($value) {
    $CI = &get_instance();
    $var = "";
    $art_info = _articles_file($value['os_articles_pdf'], $value['os_articles_id']);
    if(!empty($art_info['path']) && __is_loggedin()){                
      $img_param = array(
      'src' => 'images/download.png',
      'title' => 'Download '. filter_data($value['os_articles_title'],'display'),
      'alt' => 'Download '. filter_data($value['os_articles_title'],'display'),
      'width' => 130,
      'height' => 44,
      'border' => 0
      );
      
      //only mem_access_dwl will check how many downloads are rnremaining      
      if(__chk_mem_dwl()){
        $str = img($img_param);      
        $var .= '<a href="javascript:;" ';
        $var .= ' title="Download '.filter_data($value['os_articles_title']).'" ';
        $var .= ' onclick="javascript:set_dwl(\''.$value['os_articles_pdf'].'\',\''.$value['os_articles_id'].'\',\''.site_url('downloads/articles').'\');">';
        $var .= $str;
        $var .= '</a>';
      }      
    }
    return $var;
  }
}

// ------------------------------------------------------------------------

/**
 * Check Members Dwl
 * 
 * @access	public
 * @param   NA
 * @return	void
 * 
 */
if (!function_exists('__chk_mem_dwl')) {

  function __chk_mem_dwl() {
    $CI = &get_instance();
    $var = FALSE;
    //only mem_access_dwl will check how many downloads are rnremaining      
    $usr_info = __get_fuser();

    $qry = "SELECT m.os_mem_reg_type as `type`
            FROM " . TBL_MEMBERS . " m 
            WHERE m.os_mem_id = '".$usr_info['usr_id']."'";
    //__debug($qry);exit;
    $set = run_cust_qry($qry, TRUE);
    $type = $set['list']['type'];

    $mem_access_dwl = $CI->config->item('mem_access_dwl');
    if(isset($mem_access_dwl[$type])){
      $dwl_per_sess = $mem_access_dwl[$type];
    }else{
      $dwl_per_sess = $mem_access_dwl['other'];
    }

    $qry = "SELECT count(d.os_mem_dwl_id) as `cnt`
            FROM " . TBL_MEM_DWL . " d              
            WHERE d.os_mem_id = '".$usr_info['usr_id']."'
            AND DATE_FORMAT(d.os_mem_dwl_dt, '%d-%m-%Y') = '".  date('d-m-Y',now())."'
            ";
    //__debug($qry);exit;
    $set = run_cust_qry($qry,TRUE);
    if ($set['num_rows'] > 0) {
      $cnt_dwl = $set['list']['cnt'];
      if($cnt_dwl < $dwl_per_sess){
        $var = TRUE;
      }
    }
    return $var;
  }
}

// ------------------------------------------------------------------------

/**
 * Btn Subscribe - based on coutry session
 * 
 * @access	public
 * @param   string
 * @param   int
 * @return	void
 * 
 */
if (!function_exists('__btn_sub')) {

  function __btn_sub($value) {
    $CI = &get_instance();
    $var = "";
    $img_param = array(
                  'src' => 'images/subscribe.png',
                  'title' => 'Subscribe '. filter_data($value['os_articles_title'],'display'),
                  'alt' => 'Subscribe '. filter_data($value['os_articles_title'],'display'),
                  'width' => 130,
                  'height' => 44,
                  'border' => 0
                  );
      $str = img($img_param);      
      $var .= '<a href="javascript:;" ';
      $var .= ' title="Subscribe '.filter_data($value['os_articles_title']).'" ';
      $var .= ' onclick="javascript:fn_adda(\''.($value['os_articles_id']).'\');">';
      $var .= $str;
      $var .= '</a>';
    return $var;
  }
}


// ------------------------------------------------------------------------

/**
 * Set Post Data
 * 
 * @access	public
 * @param   void
 * @return	array
 * 
 */
if (!function_exists('__set_post_data')) {
	function __set_post_data()
	{
    $CI = &get_instance();
    $var = array();    
    if(isset($_POST)){      
      foreach($_POST as $key => $val){
        $var[$key] = $CI->input->post($key); 
      }
    }    
    return $var;
  }
}
  
// ------------------------------------------------------------------------

/**
 * Build Admin Links
 * 
 * @access	public
 * @param   NA
 * @return	void
 */
if (!function_exists('build_admin_links')) {

  function build_admin_links() {
    $var = '';
    $limit = 20;
    $qry = "SELECT os_bk_mod_title as `title`, os_bk_mod_url as `url`
      FROM " . TBL_BK_MODULES . "
      WHERE os_bk_mod_status = '1'
      ORDER BY os_bk_mod_title
      LIMIT $limit 							  			
      ";
    $set = run_cust_qry($qry);
    if ($set['num_rows'] > 0) {
      foreach ($set['list'] as $key => $value) {
        $var .= ' <li><a href="' . site_url($value['url']) . '">' . $value['title'] . '</a></li>  ';
      }
    }
    return $var;
  }

}


// ------------------------------------------------------------------------

/**
 * Journal Access
 * 
 * @access	public
 * @param   void
 * @return	array
 * 
 */
if (!function_exists('__j_access')) {
	function __j_access($id = 0, $access = '')
	{
    $var = FALSE;
    $CI = &get_instance();
    /*Type of asccess journals, 1 = Paid Access, 2 = Open Access */
    if($access == 2){
      $var = TRUE;
    }else{
      /*if(__is_loggedin()){
        $usr_info = $CI->session->userdata('usr_info');
        $jor_v = $usr_info['jor_v'];
        if(!empty($jor_v)){
          $jor_v_arr = explode(',', $jor_v);
          if(in_array($id, $jor_v_arr)){
            $var = TRUE;
          }
        }
      }*/
      $var = TRUE;
    }
    
    return $var;
  }
}

// ------------------------------------------------------------------------

/**
 * Article Access
 * 
 * @access	public
 * @param   void
 * @return	array
 * 
 */
if (!function_exists('__a_access')) {
	function __a_access($id = 0, $access = '')
	{
    $var = FALSE;
    $CI = &get_instance();
    /*Type of asccess journals, 1 = Paid Access, 2 = Open Access */
    if($access == 2){
      $var = TRUE;
    }else{
      if(__is_loggedin()){
        $usr_info = $CI->session->userdata('usr_info');
        $art_v= $usr_info['art_v'];
        if(!empty($art_v)){
          $art_v_arr = explode(',', $art_v);
          if(in_array($id, $art_v_arr)){
            $var = TRUE;
          }
        }
      }
    }
    
    return $var;
  }
}


// ------------------------------------------------------------------------

/**
 * Email Styles
 * 
 * @access	public
 * @param   void
 * @return	array
 * 
 */
if (!function_exists('__email_styles')) {

  function __email_styles() {
    $var = array();
    $var['com'] = "
  font-family: Arial,Helvetica,sans-serif;font-size: 13px;font-weight: normal;
  line-height: 18px;text-decoration: none;";
    $var['menu'] = "color: #fff;font: Arial,Helvetica,sans-serif; font-size: 13px;text-decoration: none;
  font-family: Arial,Helvetica,sans-serif;font-size: 13px;font-weight: bold;
  line-height: 18px;text-decoration: none;";
    $var['a'] = "color: #2F5A81;font: Arial,Helvetica,sans-serif; font-size: 13px;text-decoration: none;
  font-family: Arial,Helvetica,sans-serif;font-size: 13px;font-weight: normal;
  line-height: 18px;text-decoration: none;";
    $var['p'] = "color: #000;font: Arial,Helvetica,sans-serif; font-size: 12px;text-decoration: none;
  font-family: Arial,Helvetica,sans-serif;font-size: 13px;font-weight: normal;
  line-height: 18px;text-decoration: none;";
    $var['txtbox'] = "width: 250px; border: 1px solid #4F4F4F; padding: 2px; margin: 0 2px 8px 2px;color: #000;font: Arial,Helvetica,sans-serif; font-size: 12px;text-decoration: none;
  font-family: Arial,Helvetica,sans-serif;font-size: 13px;font-weight: normal;
  line-height: 18px;text-decoration: none;";
    
    return $var;
  }

}


// ------------------------------------------------------------------------

/**
 * Get Cart Quantity
 * 
 * @access	public
 * @param   void
 * @return	array
 * 
 */
if (!function_exists('__get_cart_qty')) {

  function __get_cart_qty() {
    $var = 0;
    $CI = &get_instance();
    $ses_id = $CI->session->userdata('cart_ses_id');
    $cart = $CI->session->userdata('cart');    
    
    if(isset($cart[$ses_id]) && is_array($cart[$ses_id])){
      $var = count($cart[$ses_id]);
    }
    return $var;
  }

}

/* End of file project_helper.php */
/* Location: ./system/helpers/project_helper.php */